<template>
  <div class="dashboard">
    <div class="dashboard-header">
      <h1>欢迎回来，{{ authStore.user?.username }}</h1>
      <button @click="handleLogout" class="btn btn-primary">登出</button>
    </div>
    
    <div class="dashboard-stats">
      <div class="stat-card">
        <div class="stat-icon">🔑</div>
        <div class="stat-info">
          <div class="stat-value">{{ stats.totalTokens }}</div>
          <div class="stat-label">总令牌数</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">⭐</div>
        <div class="stat-info">
          <div class="stat-value">{{ stats.favorites }}</div>
          <div class="stat-label">收藏</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">📁</div>
        <div class="stat-info">
          <div class="stat-value">{{ stats.categories }}</div>
          <div class="stat-label">分类</div>
        </div>
      </div>
    </div>
    
    <div class="quick-actions">
      <router-link to="/tokens" class="action-btn">
        <span class="action-icon">🔐</span>
        <span>管理令牌</span>
      </router-link>
      
      <router-link to="/settings" class="action-btn">
        <span class="action-icon">⚙️</span>
        <span>设置</span>
      </router-link>
      
      <router-link v-if="authStore.isAdmin" to="/admin" class="action-btn admin">
        <span class="action-icon">👑</span>
        <span>管理员面板</span>
      </router-link>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/store/auth';
import { useTokenStore } from '@/store/tokens';
import { useUserStore } from '@/store/user';

const router = useRouter();
const authStore = useAuthStore();
const tokenStore = useTokenStore();
const userStore = useUserStore();

const stats = ref({
  totalTokens: 0,
  favorites: 0,
  categories: 0,
});

async function loadData() {
  try {
    const [tokensData, categories] = await Promise.all([
      tokenStore.fetchTokens(),
      userStore.fetchCategories(),
    ]);
    
    stats.value.totalTokens = tokensData.total;
    stats.value.favorites = tokensData.tokens.filter(t => t.isFavorite).length;
    stats.value.categories = categories.length;
  } catch (error) {
    console.error('Failed to load dashboard data:', error);
  }
}

async function handleLogout() {
  await authStore.logout();
  router.push('/login');
}

onMounted(() => {
  loadData();
});
</script>

<style scoped>
.dashboard {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.dashboard-header h1 {
  font-size: 32px;
  color: var(--text-color);
}

.dashboard-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 40px;
}

.stat-card {
  background: var(--bg-color);
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  box-shadow: var(--shadow);
}

.stat-icon {
  font-size: 48px;
}

.stat-value {
  font-size: 36px;
  font-weight: bold;
  color: var(--primary-color);
}

.stat-label {
  font-size: 14px;
  color: var(--text-secondary);
}

.quick-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.action-btn {
  background: var(--bg-color);
  border-radius: 12px;
  padding: 32px 24px;
  text-align: center;
  text-decoration: none;
  color: var(--text-color);
  transition: all 0.3s;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.action-btn:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.action-icon {
  font-size: 48px;
}

.action-btn.admin {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}
</style>


